package com.atguigu.jxc.service.impl;

import com.atguigu.jxc.dao.SupplierDao;
import com.atguigu.jxc.domain.ServiceVO;
import com.atguigu.jxc.entity.Goods;
import com.atguigu.jxc.entity.Supplier;
import com.atguigu.jxc.service.SupplierService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class SupplierServiceImpl implements SupplierService {
    @Autowired
    private SupplierDao supplierDao;
    //获取供应商列表
    @Override
    public Map<String, Object> supplierList(Integer page, Integer rows, String supplierName) {
        Map<String, Object> map = new HashMap<>();
        page = page == 0 ? 1 : page;
        int offSet = (page - 1) * rows;
        List<Supplier> list= supplierDao.getListInventory(rows,supplierName,offSet);
        int size = list.size();
        map.put("total",size);
        map.put("rows",list);
        return map;
    }

    //添加或修改供应商
    @Override
    public ServiceVO saveOrUpdate(Integer supplierId, Supplier supplier) {
        int count=0;
        if(supplierId==null){
         count= supplierDao.save(supplier);
        }else {
          count=supplierDao.update(supplier);
        }
        //判断是否成功
        if(count>0){
            return new ServiceVO(100,"请求成功");
        }
        return new ServiceVO<>(250,"周傲");
    }

    //删除供应商
    @Transactional
    @Override
    public ServiceVO delete(String ids) {
        int count=0;
        String[] split = ids.split(",");
        for (String s : split) {
            count= supplierDao.delete(s);
            }
        if(count>0){
            return new ServiceVO(100,"请求成功");
        }
        //判断是否成功
        return new ServiceVO<>(250,"周傲");
    }

}
